import {defineStore} from "pinia";
import {SearchPageApi} from "../api/SearchPageApi.js";
import {FMessage} from '@fesjs/fes-design';

export const useSearchPageStore = defineStore('useSearchPageStore', {
    state: () => ({
        menu: [],
        columns: [],
        tableData: [],
        searchCondition: {
            searchOptions: [],
            searchOptionValue: '',
            searchValue: '',
        },
        tablePageInfo: {
            currentPage: 1,
            pageTotalCount: 0,
            pageSize: 10
        },
        show: {
            showDetail: false,
            rowId: null,
            detailContent: "",
        }
    }),
    actions: {
        async selectMenu(value) {
            const menuItem = this.menu.filter(c => c.value === value.value);
            if (menuItem.length > 0) {

                this.columns = menuItem[0].tableColumnList.map(t => {
                    if (t.prop === "op") {
                        return {
                            ...t, action: [
                                {
                                    label: '详情',
                                    func: (row) => {
                                        this.show.detailContent = "";
                                        this.show.rowId = row.id;
                                        this.show.showDetail = true;
                                    },
                                }
                            ]
                        }
                    } else {
                        return {...t}
                    }
                });
            }

            const requestParam = {
                searchTypeValue: this.searchCondition.searchOptionValue,
                searchValue: this.searchCondition.searchValue,
                itemValue: value.value,
                pageNumber: this.tablePageInfo.currentPage,
                pageSize: this.tablePageInfo.pageSize
            }
            const pageData = await SearchPageApi.dataList(requestParam)
            this.tableData = pageData.rows
            this.tablePageInfo.pageTotalCount = pageData.pageTotalCount
            this.tablePageInfo.currentPage = 1
            this.tablePageInfo.pageSize = 10
        },
        async searchData() {

            if (this.searchCondition.searchOptionValue === "" || this.searchCondition.searchValue === "") {
                FMessage.error("请输入查询条件")
                return;
            }
            this.menu = []
            this.columns = []
            this.tableData = []

            const requestParam = {
                searchTypeValue: this.searchCondition.searchOptionValue,
                searchValue: this.searchCondition.searchValue
            }
            this.menu = await SearchPageApi.searchMenuInit(requestParam);
        },
        async initSearchPage() {
            const data = await SearchPageApi.requestInitSearchPageData();
            this.searchCondition.searchOptions = data.searchOptions;
        },
        async pageChange(currentPage, pageSize, item) {
            const requestParam = {
                searchTypeValue: this.searchCondition.searchOptionValue,
                searchValue: this.searchCondition.searchValue,
                itemValue: item,
                pageNumber: currentPage,
                pageSize: pageSize

            }
            const pageData = await SearchPageApi.dataList(requestParam)
            this.tableData = pageData.rows
        },
        async openDetail(itemValue) {
            const detailData = await SearchPageApi.requestDetail(itemValue,this.show.rowId);
            this.show.detailContent = JSON.stringify(detailData, null,4);
        },
        reset() {
            this.menu = []
            this.columns = []
            this.tableData = []
            this.searchCondition.searchValue = ''
        }
    }
})